#include "Arrow.hpp"
#include "Vec3f.hpp"
#include "glm.h"

using namespace siut;

static GLMmodel* _arrow_model = NULL;

Arrow::Arrow()
{
	speed_ = 300;
	_done = false; 
	_fired = false;
}

Arrow::~Arrow()
{

}

void Arrow::privateInit()
{

	//if(!_arrow_model)
	//	{
	//		_arrow_model = glmReadOBJ("arrow.obj");
	//		if (!_arrow_model)
	//			exit(0);

	//		glmUnitize(_arrow_model);
	//		glmFacetNormals(_arrow_model);
	//		glmVertexNormals(_arrow_model, 90);
	//	}

	//	id_list = glGenLists(1);
	//	glNewList(id_list,GL_COMPILE);
	//	glmDraw(_arrow_model,GLM_SMOOTH | GLM_TEXTURE | GLM_MATERIAL  );
	//	glEndList();

	
}

void Arrow::privateRender()
{
	if(_fired && !_done)
		gluSphere(gluNewQuadric(),1,20, 30 );
}



void Arrow::privateUpdate()
{
	if(_fired)
	{
		Vec3f pos =  Vec3f ( matrix_[12], matrix_[13], matrix_[14]); 

		Vec3f a = _target - pos;

		if( length(a) <= 1.9 )
		{
				_done = true; 
				_fired = false;
				_done = false;
		}
		a.normalize();
		matrix_.translate(a * speed_ * sslf_);
	}
}
void Arrow::setStart(siut::Vec3f st)
{
	if(!_fired)
	{
		std::cout << "Start set" << std::endl;
	
	  matrix_[12] = st[0]; 
	  matrix_[13] = st[1] + 30 ;
	  matrix_[14] = st[2]; 
	}
}

void Arrow::setEnd(siut::Vec3f st)
{
	if(!_fired)
	 {
		 std::cout << "Start set" << std::endl;
		 _target = st;
	 }
}



